package br.ufrgs.meetapp.activities;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import br.ufrgs.meetapp.models.Buddy;

import com.google.android.maps.GeoPoint;

public class TrackOthersTabActivity extends TabActivity implements OnClickListener{

	public static final int TAB_MAP = 0;
	public static final int TAB_LIST = 1;
	
	public static ArrayList<Buddy> buddyList;
	public static Buddy buddySelected;
	public static boolean isBuddySelected;
	public static GeoPoint myLocation;
	public static ArrayList<Integer> filteredIds;
	
	private Context context;
	private TabHost tabHost;
	
	public void onCreate(Bundle savedInstanceState) {
	    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.track_others_tab_activity);

	    buddyList = new ArrayList<Buddy>();
	    buddySelected = null;
	    filteredIds = new ArrayList<Integer>();
	    
	    context = this;
	    
	    setTitleBar();
	    
	    tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, TrackOthersMapActivity.class);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec("map").setIndicator("Map").setContent(intent);
	    tabHost.addTab(spec);
	    
	    // Do the same for the List tab
	    intent = new Intent().setClass(this, TrackOthersListActivity.class);
	    spec = tabHost.newTabSpec("list").setIndicator("List").setContent(intent);
	    tabHost.addTab(spec);

	    tabHost.setCurrentTab(TAB_MAP);
	}
	
	/***
	 * Sets the top bar
	 */
	private void setTitleBar() {
		// set titleBar
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
		TextView titleBarTitle = (TextView) findViewById(R.id.title_bar_text_title);
		titleBarTitle.setText(R.string.tracker_others_tab_title);
//		Button buttonTitleBar = (Button) findViewById(R.id.title_bar_button);
//		buttonTitleBar.setVisibility(View.VISIBLE);
//		buttonTitleBar.setOnClickListener(this);
	}
	
	/**
	 * Switch tab
	 * 
	 * @param tab to navigate
	 */
	public void switchTab(int tab){
        tabHost.setCurrentTab(tab);
	}

	@Override
	public void onClick(View view) {
		Intent intent;
		switch (view.getId()) {
		default:
			break;
		}	
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	/**
	 * Calculates the distance between two geopoints
	 * @return String containing the distance between the two locations and an empty string if one location is null
	 */
	public static String getDistanceBetweenTwoGeoPoints(GeoPoint geoPointA, GeoPoint geoPointB){
		String distanceString = new String();
		if(geoPointA != null && geoPointB != null){
			Location locationA = new Location("Point A");

			locationA.setLatitude(geoPointA.getLatitudeE6() / 1E6);
			locationA.setLongitude(geoPointA.getLongitudeE6() / 1E6);
			
			Location locationB = new Location("Point B");

			locationB.setLatitude(geoPointB.getLatitudeE6() / 1E6);
			locationB.setLongitude(geoPointB.getLongitudeE6() / 1E6);
			
			double distance = locationA.distanceTo(locationB);
			if(distance < 1050){
				distanceString = "Distance: " + (int)distance + "m";
			} else {
				DecimalFormat decimalFormat = new DecimalFormat("#.0");
				distanceString = "Distance: " + decimalFormat.format(distance/1000) + "km";
			}
			
		}
		return distanceString;
	}
}

